<?php

App::uses('AppController', 'Controller');

class ProductsController extends AppController {

    public $uses = array('Group', 'Product');
    
    public $paginate = array (
        'limit' => 15
    );

    public function index($groupKey) {
        $group = $this->Group->find('first', array(
            'conditions' => array('Group.key' => $groupKey)
        ));
        if (! $group) {
            $this->redirect("/");
        }
        
		$this->pageTitle = $group['Group']['name'];
        $products = $this->paginate('Product', array(
            'Product.group_id' => $group['Group']['id']
        ));
        
        $this->set(compact('group', 'products', 'groupKey'));
    }
    
    public function view($groupKey, $id) {
        $product          = $this->Product->findById($id);
        $this->pageTitle  = $product['Product']['name'];
        $similar_products = $this->Product->find('all', array(
            'conditions' => array(
                'Product.group_id' => $product['Group']['id'],
                'Product.id !='    => $id
            ),
            'order'      => 'RAND()',
            'limit'      => 7
        ));
        
        $this->set(compact('product', 'groupKey', 'similar_products'));
    }

}

?>